import time
from base.base import Base
from selenium.webdriver.common.by import By
from untils.connection_data import connections

class LoginPage(Base):

    def get_all_login_data(self):
        #连接
        db = connections(
            host="43.138.43.16",
            user="root",
            password="Root@2025",
            database="roncoo_mini_pay_demo"
        )
        # 查询所有账号密码
        result = db.select("SELECT username, password FROM rp_user_login")
        db.clear()
        #返回查到的账号和密码
        return result

    loginName_loc=(By.NAME,'loginName')
    # 账号
    def input_username(self, username):
        self.send_keys(self.loginName_loc, username)

    roncooPwd_loc=(By.NAME,'roncooPwd')
    # 密码
    def input_password(self, password):
        self.send_keys(self.roncooPwd_loc, password)

    login_btn_loc=(By.XPATH,'//*[@id="form1"]/div[3]/input')
    # 点击登录
    def click_login_btn(self):
        self.click(self.login_btn_loc)

    # 退出系统获取文字          //*[@id="header"]/div/ul[1]/li[2]/a   xiangduei
    back_loc = (By.XPATH, '//*[@id="header"]/div/ul[1]/li[2]/a')
    def back(self):
        self.text(self.back_loc)


    def longguo_login(self):
        # 打开网址
        self.open_url("http://43.138.43.16:7009/roncoo-pay-web-boss/login")
        # 输入账号
        self.input_username('admin')

        self.input_password('123456')

        self.click_login_btn()
        print(self.back())



    def data_driven_login_test(self):
        #将账户和密码赋值给all_data
        all_data = self.get_all_login_data()
        # 判断是否获取账号和密码
        if not all_data:
            print("数据库为空")
            return

        success = 0
        total = len(all_data)

        #循环
        for i, (username, password) in enumerate(all_data, 1):
            print(f"测试第{i}组数据")
            print(f"当前测试账号:{username},密码:{password}")

            # 打开登录页
            self.open_url("http://43.138.43.16:7009/roncoo-pay-web-boss/login")
            time.sleep(3)

            # 输入账号密码
            self.input_username(username)
            time.sleep(3)
            self.input_password(password)
            time.sleep(3)
            self.click_login_btn()
            time.sleep(3)

            # 判断登录是否成功
            if "login" not in self.driver.current_url:
                print(f"账号{username}登录成功")
                success += 1
            else:
                print(f"账号{username}登录失败")

        # 4. 显示测试总结
        print(f"\n---------测试结束-------------")
        print(f"总测试组数:{total}")
        print(f"成功组数:{success}")
        print(f"失败组数:{total-success}")









